var aimip = "100";
var port = "8010";
var host = "http://192.168.10." + aimip + ":" + port;
var wshost = "ws://192.168.10." + aimip + ":" + port + "/websocket";
var Web_socket;

(function init() {
	var path = "GetPageHead";
	host = host + path;
	$.get(
		host,
		function(data) {
			$("#PageHead").text(data.ID + "&nbsp;&nbsp;&nbsp;&nbsp;" + data.Time);
		}
	)
})()



function GetNowTime() {
	var myDate = new Date();
	var year = myDate.getFullYear(); //获取当前年
	var month = myDate.getMonth() + 1; //获取当前月
	var date = myDate.getDate(); //获取当前日
	var h = myDate.getHours(); //获取当前小时数(0-23)
	var m = myDate.getMinutes(); //获取当前分钟数(0-59)
	var s = myDate.getSeconds();
	var now = year + '-' +
		getNow(month) + "-" +
		getNow(date) + " " +
		getNow(h) + ':' +
		getNow(m) + ":" +
		getNow(s);
	document.getElementById("time").innerText = now;

}

function getNow(s) {
	return s < 10 ? '0' + s : s;
}


var recdata = "";
showjs = {
	"温度": {
		"要素名": "温度",
		"图标": "qiwen-",
		"传感器名称": "ROTRONIC",
		"地址": "TT",
		"要素值1": "TT",
		"method": "1",
	},
	"湿度": {
		"要素名": "湿度",
		"图标": "sd",
		"传感器名称": "ESP",
		"地址": "RH",
		"要素值1": "RH",
		"method": "1",

	},
	"雨量": {
		"要素名": "雨量值",
		"图标": "yuliang",
		"传感器名称": "SL-3",
		"地址": "RF",
		"要素值1": "RF",
		"method": "1",
	},
	"风向": {
		"要素名": "风向",
		"图标": "fengxiangdai",
		"传感器名称": "ROTRONIC",
		"地址": "WD",
		"要素值1": "WD",
		"method": "1",
	},
	"风速": {
		"要素名": "风速",
		"图标": "fengsu",
		"传感器名称": "ROTRONIC",
		"地址": "WS",
		"要素值1": "WS",
		"method": "1",
	},
	"气压": {
		"要素名": "气压值",
		"图标": "lishitianqi-qiya",
		"传感器名称": "SETRA270",
		"地址": "PP",
		"要素值1": "PP",
		"method": "1",
	},
//	"地温": {
//		"要素名": "地温",
//		"图标": "haishuiwendu",
//		"传感器名称": "ESP",
//		"地址": "ST0",
//		"要素值1": "ST0",
//		"method": "0.1",
//	},
}

unite = {
	"datetime": "#val",
	"TT": "#val℃",
//	"ST0": "#val℃",
	"RH": "#val%",
	"PP": "#valhPa",
	"WD": "#val°",
	"WS": "#valm/s",
	"RF": "#valmm",
	"RF": "#valmm",

	"THBAT": "#val%",
	"RFBAT": "#val%",
	"WDBAT": "#val%",
	"PPBAT": "#val%",
	"ZJBAT": "#val%",
}

method = {
	"TT": 0.1,
//	"ST0": 0.1,
	"RH": 1,
	"PP": 0.1,
	"WS": 0.1,
	"RF": 0.1,
	"AT": 0.1,
	"DC": 0.1,

}
//jsondata = {};

function getdatashow(jsondata) {
	if (!jsondata.hasOwnProperty("DT")) {
		return;
	}
	showvalue = {};
	
	console.log(showjs);
	$.each(showjs, function(ysname, ysvalue) {
		recdata = jsondata
		 console.log(ysname)
		showvaluetemp = {};
//		ysvalue=JSON.parse(ysvalue)
		$.each(ysvalue, function(index, value) {
			
			if (index.indexOf("值") == -1) {
				showvaluetemp[index] = value;

			} else {
				temp = unite[value];
				temp = temp.replace("#val", recdata[value])

				showvaluetemp[index] = temp;

			}
		})
		showvalue[ysname] = showvaluetemp;
	})
	// console.log(showvalue);''
	 console.log(showvalue);
	document.getElementById("rain").innerHTML = showvalue["雨量"]["要素值1"];
	document.getElementById("zhname").innerHTML = "站号:"+jsondata["ZH"];	
	document.getElementById("time").innerHTML = "时间:"+jsondata["DT"];	
	document.getElementById("wind_1").innerHTML = showvalue["风向"]["要素值1"];
	document.getElementById("wind_2").innerHTML = showvalue["风速"]["要素值1"];
	document.getElementById("tt_1").innerHTML = showvalue["温度"]["要素值1"];
	document.getElementById("rh_1").innerHTML = showvalue["湿度"]["要素值1"];
	document.getElementById("pp_1").innerHTML = showvalue["气压"]["要素值1"];
	
	$("#APPstatus").attr("class", "label label-success");
	$("#APPstatus").html("正常");
	
	
	// document.getElementById("st_1").innerHTML = showvalue["地温"]["要素值1"];
//	check_status(jsondata);
}


function check_status(e) {

	if (parseFloat(e["AT"]) > 60 || parseFloat(e["DC"]) < 10.8 || parseFloat(e["DC"]) > 13.8 || socket_status==0) {
		$("#APPstatus").attr("class", "label label-danger");
		$("#APPstatus").html("异常");
	} else {
		$("#APPstatus").attr("class", "label label-success");
		$("#APPstatus").html("正常");
	}
	let senstau = 0;
	
	$.each(e, function(index, value) {
		
		if (index.indexOf("PP") > -1 || index.indexOf("RF") > -1 || index.indexOf("RH") > -1 ||
			index.indexOf("TT") > -1 || index.indexOf("WD") > -1 || index.indexOf("WS") > -1
		) {
			if (parseFloat(value) == 0 || parseFloat(value) == 888.8 || parseFloat(value) == 111) {
				senstau++;
			}
		}

	});
	if (senstau > 0) {
		
		$("#Sensorstatus").attr("class", "label label-danger");
		$("#Sensorstatus").html("异常");
	} else {
		$("#Sensorstatus").attr("class", "label label-success");
		$("#Sensorstatus").html("正常");
	}

}
